<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{
				background-color: transparent !important; height:100%;
			}
			.info_box{ position: fixed; left: 0; bottom: 0; width:100%;  box-sizing: border-box; padding: 0; z-index: 12;}
			.btn01{ display:block; border: 0;  width: 100%; height: 50px; font-size: 15px; color: #666;}
			.info_box .pay_btn .pay_btn01 button { position: relative; }
			.info_box .pay_btn .pay_btn01 button:not(:last-child)::after{display:block; position: absolute; content: ""; left: 0; right: 0; bottom: 0; height: 1px; background-color: #dedede; overflow: hidden; transform: scaleY(0.5); -webkit-transform: scaleY(0.5);}
			.btn{ display:block; border: 0; background-color: #7dc3e8; width: 100%; height: 44px; font-size: 15px; color: #fff; border-radius: 4px;}

			.a-active { color:#3262F1 !important; }
			.pay_btn{ padding: 0 15px 10px;}
			.pay_btn01 { border:0;background-color:#fff; box-sizing: border-box; border-radius: 4px;overflow:hidden;}
			.pay_btn .btn{ background-color:#fff; color:#666; font-size:15px;}
			.btn02{ background-color:#fff; color:#666 !important; font-size:15px;}
			
			.pay_nums_list { padding: 0 15px; }
			.pay_nums_list li{ position: relative; }
			.pay_nums_list li::after{ display:block; position: absolute; content: ""; left: 0; right: 0; bottom: 0; height: 1px; background-color: #dedede; overflow: hidden; transform: scaleY(0.5);}
			.pay_nums_list img{ width:25px; margin-right: 12px;}
			.pay_nums_list h4{ display:block; font-size: 15px; color: #333; font-weight: 400; line-height: 50px;}
			.pay_nums_list i{ font-size:13px;}
			.pay_nums_list li.on{ background:url(../../image/img_25.png) no-repeat right center; background-size: 14px auto;}

		</style>
	</head>
	<body>
		<div id="filter" style="height:100%;">
		
		<!--完善信息弹窗start-->
		<div class="info_box" id="content">
			<div class="pay_btn">
				<div class="pay_btn01 pay_nums_list">
					<ul id="btnslist">
						<li class="on" tapmode data-paytype="wechat" onclick="selPay(this);">
							<div class="m_cell_hd"><img src="../../image/img_wx.png" /></div>
							<div class="m_cell_ft m_cell_primary"><h4>微信支付</h4></div>
						</li>
						<li tapmode data-paytype="alipay" onclick="selPay(this);">
							<div class="m_cell_hd"><img src="../../image/img_alipay.png" /></div>
							<div class="m_cell_ft m_cell_primary"><h4>支付宝</h4></div>
						</li>
						<!-- <li tapmode data-paytype="union" onclick="selPay(this);"> -->
							<!-- <div class="m_cell_hd"><img src="../../image/img_union.png" /></div> -->
							<!-- <div class="m_cell_ft m_cell_primary"><h4>银联支付</h4></div> -->
						<!-- </li> -->
					</ul>
				</div>
			</div>
			<div class="pay_btn">
			  <button class="btn btn02" tapmode id="login-btn" onclick="pay();">立即支付</button>
			</div>
		</div>
		<!--完善信息弹窗end-->
		
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js" ></script>
	<script type="text/javascript" src="../../script/app.js" ></script>
	<script type="text/javascript">
		var fromfrm;
		var paytype = 'wechat';
		var paymoney = 0,tradeid = 0,ordertype = 0;
		apiready = function() {
			api.parseTapmode();
			//点击空白处移除Frame
			fromfrm = api.pageParam.fromfrm;
			paymoney = api.pageParam.paymoney;
			tradeid = api.pageParam.tradeid;
			ordertype = api.pageParam.ordertype;
			var myDiv = document.getElementById("filter");
			myDiv.addEventListener("touchstart",function(){
				api.setFrameAttr({
					name: api.frameName,
					hidden: true
				});
				//api.closeFrame({
					//name: api.frameName
				//});
				
			});
			var myDiv1 = document.getElementById("content");
			myDiv1.addEventListener("touchstart",function(event){
				event=event||window.event;
				event.stopPropagation();
			});
		}
		function selPay(obj) {
			
			paytype = $api.attr(obj,'data-paytype');
			var liList = document.getElementById("btnslist").children;
			for (var i = 0; i < liList.length; i++) {
				$api.removeCls(liList[i], 'on')
			}
			$api.addCls(obj,'on');
			
		}
		function msgpush(msgtype) {
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: ApiUrl + '/v1/msgpush',
						method: 'post',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								msgtype: msgtype
							}
						}
					}, function(ret, err) {
						console.log(JSON.stringify(ret));
						
					});
				}
			});
		}
		function pay() {
			if($api.cssVal($api.byId('login-btn'),'opacity')!=1)
			{
				return false;
			}
			if(paymoney==0)
			{
				toast("金额不能为零");
				return false;
			}
			if(paytype=='union')
			{
				showtoast("warning","暂不支持银联支付");
				return false;
			}
			$api.css($api.byId('login-btn'),'opacity:0.99');
			closeframe();
			var url = ApiUrl + '/v1/order'+paytype;

			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					api.ajax({
						url: url,
						method: 'post',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								tradeid: tradeid,
								paymoney: paymoney,
								ordertype: ordertype
								
							}
						}
					}, function(ret, err) {
						console.log(JSON.stringify(ret));
						if(ret)
						{
							if(paytype == 'alipay')
							{
								var aliPayPlus = api.require('aliPayPlus');
								aliPayPlus.payOrder({
									orderInfo: ret.data
								}, function(ret, err) {
									if(ret.code == 9000)
									{
										if(fromfrm == 'insure') {
											setTimeout(function(){
												msgpush('insuresuc');
											}, 1000);
											api.sendEvent({
												//通知更新状态
												name : 'refreshinsure'
											});
										}
										else
										{
											api.execScript({
												name: 'goodsdetail_win',
												script: 'sendmsg();'
											});
											api.sendEvent({
												//通知更新状态
												name : 'refreshgoodlist'
											});
										}
										openAlert();
									}
									else if(ret.code == 6001)
									{
										toast('用户取消支付');
										
									}
								});
							}
							else if(paytype == 'wechat')
							{
								var wx = api.require('wx');
								wx.isInstalled(function(ret, err) {
									if (!ret.installed) {
										toast("未安装微信客户端，无法使用微信支付");
										return false;
									}
								});
								//微信支付
								var wxPay = api.require('wxPay');
								var datajson = {
									apiKey: ret.appid,
									orderId: ret.prepayid,
									mchId: ret.partnerid,
									nonceStr: ret.noncestr,
									timeStamp: ret.timestamp,
									package: 'Sign=WXPay',
									sign: ret.sign
								};
								wxPay.payOrder(datajson, function(ret, err) {
									if (ret.status) {
										if(fromfrm == 'insure') {
											setTimeout(function(){
												msgpush('insuresuc');
											}, 1000);
											api.sendEvent({
												//通知更新状态
												name : 'refreshinsure'
											});
										}
										else
										{
											api.execScript({
												name: 'goodsdetail_win',
												script: 'sendmsg();'
											});
											api.sendEvent({
												//通知更新状态
												name : 'refreshgoodlist'
											});
										}
										openAlert();
										//支付成功
									} else {
										//alert(err.code);
									}
								});
							}
							else if(paytype == 'union')
							{
								var unPay = api.require('unionPay');
								api.ajax({
									url: ApiUrl+'/union/unionpay.php',//获取tn号
									method: 'post',
									returnAll:false,
									data:{
										values: {orderId: ret.ordersn,txnAmt:ret.fee}//提交商户后台生成的订单ID 
									}
								},function(ret,err){
									console.log(JSON.stringify(err));
									if (ret.code=='ok') {
										unPay.pay({
											tn: ret.tn,
											devMode: true
										}, function(ret, err){
											if(ret.result == 'success')
											{
												api.sendEvent({
													//通知更新积分
													name : 'updatecredit'
												});
												var param = {
													wintitle: '支付结果',
													icon: 'sucess.png',
													title: '支付成功',
													msg: '运单已完成',
													btntxt: '返回',
													winname: 'billlist_win',
													changestyle: 1
												};
												opWin('paymsg_win','../public/paymsg_win.html',false,param);
												//支付成功
											}
										   //api.alert({msg:JSON.stringify(ret)});
										});
									}
								});

							}
						} else {
							console.log(JSON.stringify(err));
							toast(err.body.message);
						}
						
						$api.css($api.byId('login-btn'),'opacity:1');
					});
				}
			});
		}
		function openAlert()
		{
			var icon = 'paysuccess.png';
			var title = '支付成功';
			var msg = '';
			var winname = fromfrm+'_win';
			var btntxt = '返回';
			var param = {
				icon: icon,
				title: title,
				msg: msg,
				btntxt: btntxt,
				winname: winname,
			};
			openNavWin('#ddd','noheadmsg_win','../public/noheadmsg_frm.html','支付结果',false,true,false,[],param);
		}
		function closeframe() {
			api.setFrameAttr({
					name: api.frameName,
					hidden: true
				});
		}
	</script>
</html>